<template>
  <div>
    <el-row>
      <el-col :span="24"><top-nav-menu @select="select"></top-nav-menu></el-col>
    </el-row>
    <TextTree
      ref="leftTree"
      :tree="$store.state.leftMenu"
      @leaf-click="leafClick"
    >
      <router-view v-if="flushState"></router-view>
    </TextTree>
  </div>
</template>

<script>
import TopNavMenu from "~/views/layout/component/FirstLevelNavMenu";
import TextTree from "~/components/TextTree";
import { cofirmAction } from "../store/actionTypes";
import { mapState } from "vuex";
export default {
  name: "RootView",
  inject: ["reload"],
  data() {
    return {
      current: null,
      flushState: true,
      tree: undefined,
      mirror: null,
    };
  },
  components: {
    TextTree,
    TopNavMenu,
  },
  created() {
    this.$store.dispatch(cofirmAction.UPDATE_LEFTMENU);
    // if (document.querySelector(".el-tree-node") !== null) {
    //   let kk = document
    //     .querySelector(".el-tree-node")
    //     .getElementsByClassName("is-current");
    //   if (kk == -1) {
    //     window.location.reload();
    //   }
    // }
    if (window.location.href.indexOf("#reloaded") == -1) {
      window.location.href = location.href + "#reloaded";
      window.location.reload();
    }
  },
  methods: {
    select(index) {
      if (index !== this.current) {
        this.$router.push({ path: index });
        this.current = index;
        this.$common.treeVisible = true;
        this.flush();
      }
    },
    leafClick(d) {
      this.$set(this.$common, "region", d);
      this.flush();
    },
    flush() {
      this.flushState = false;
      this.$nextTick(() => {
        this.flushState = true;
      });
    },
  },
  mounted() {
    if (this.current) this.$router.replace(this.current);
    let treeVisible = true;
    let that = this;
    Object.defineProperty(this.$common, "treeVisible", {
      get() {
        return treeVisible;
      },
      set(v) {
        if (typeof v === "boolean" && v !== treeVisible) {
          treeVisible = v;
          if (that.$refs.leftTree) {
            that.$refs.leftTree.$forceUpdate();
          }
        }
      },
    });
    this.$nextTick().then(() => {
      let int = setInterval(() => {
        if (document.querySelector(".el-tree-node") !== null) {
          let kk = document
            .querySelector(".el-tree-node")
            .getElementsByClassName("el-tree-node__content")[2]
            .click();
          clearInterval(int);
        }
      });
    });
  },
  computed: {},
};
</script>

<style scoped>
</style>